package edu.scau.mis.controller.user;

import edu.scau.mis.core.controller.ResultEntity;
import edu.scau.mis.core.utils.WxUserInfoUtils;
import edu.scau.mis.rest.entity.RestUser;
import edu.scau.mis.rest.service.RestUserService;
import edu.scau.mis.sys.security.service.TokenService;
import edu.scau.mis.sys.service.SysRoleService;
import edu.scau.mis.sys.service.SysUserRoleService;
import edu.scau.mis.sys.utils.HttpServletRequestUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    TokenService tokenService;

    @Autowired
    RestUserService restUserService;

    @Autowired
    SysUserRoleService sysUserRoleService;

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    HttpServletRequest httpServletRequest;

    @PostMapping("/static-login")
    public ResultEntity staticLogin(@RequestParam("code") String code){
        //通过code换取信息
        JSONObject resultJson = WxUserInfoUtils.getResultJson(code);

        if (resultJson.has("openid")){
            //获取sessionKey和openId
            String sessionKey = resultJson.get("session_key").toString();
            String openid = resultJson.get("openid").toString();

            //生成自定义登录态session
            String session = null;
            Map<String,String> sessionMap = new HashMap<>();

            sessionMap.put("sessionKey",sessionKey);
            sessionMap.put("openid",openid);
            session = JSONObject.fromObject(sessionMap).toString();

            //生成token
            String token = tokenService.getToken(session);

            //查询用户是否存在
            RestUser restUser = restUserService.queryRestUserByOpenid(openid);
            System.out.println(restUser);
            if (restUser != null){
                return ResultEntity.success("登录成功", token); //用户存在，返回结果
            }else { //用户不存在，新建用户信息
                RestUser user = new RestUser();
                user.setOpenid(openid);
                restUserService.insert(user);

                //获取角色id
                long roleId = this.sysRoleService.queryByCode("normal").getId();

                //新增用户角色
                this.sysUserRoleService.insertRoleUser(user.getId(), roleId);
                return ResultEntity.success("登录成功", token);
            }

        }
        return ResultEntity.error("授权失败："+ resultJson.getString("errmsg"));
    }

    @PostMapping("/login")
    public ResultEntity login(@RequestBody Map<String, String> map){
        //获取map里的参数
        String nickname = map.get("nickname");
        String avatarUrl = map.get("avatarUrl");

        //获取请求头token
        String openid = HttpServletRequestUtils.getOpenid(httpServletRequest);

        RestUser restUser = new RestUser();
        restUser.setOpenid(openid);
        restUser.setNickname(nickname);
        restUser.setAvatarUrl(avatarUrl);

        restUserService.update(restUser);

        return ResultEntity.error("操作成功");
    }

}
